package org.example;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class UnicodeUtils {
    public static String toUnicode(String text) {
        StringBuilder sb = new StringBuilder();
        for (char c : text.toCharArray()) {
            if (c <= 127) {
                sb.append(c);
            } else {
                sb.append(String.format("\\u%04x", (int) c));
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) throws IOException {
        // 读取 UTF-8 文件
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(
                new FileInputStream("input.txt"), StandardCharsets.UTF_8));
             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                     new FileOutputStream("output.txt"), StandardCharsets.UTF_8))) {

            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("line:" + line);
                writer.write(toUnicode(line));
                writer.newLine();
            }
        }
        System.out.println("转换完成！");
    }
}
